

#include "MyDisplay.h"



MyDisplay :: MyDisplay(const MyDisplay & another){

        //cout <<"Copy  My display called...."<<endl;
        (*this) = another;  
}

MyDisplay :: MyDisplay()
{

       // cout <<"Default My display called...."<<endl;
	displayName = getenv("Dispaly");
	display = XOpenDisplay(displayName);
	if(display == NULL)
	{
		cout << "Error in display ...." << endl;
		exit(1);
	}
	
	// get the geometry of the default screen for our display. 
	screenNo = DefaultScreen(display);
	displayWidth = DisplayWidth(display,screenNo);
	displayHeight = DisplayHeight(display,screenNo);
	
	createWindow(getWidth(), getHeight(), 0, 0);
        createGC(0);
 
	
}

void MyDisplay :: closeDisplay()
{
	XCloseDisplay(display);
}



void MyDisplay :: displayLine(int width,int height)
{
	
	//XPutImage(display, win, gc, image, 0, 0, 0, 0, width, height);
	XFlush(display);
}



void MyDisplay :: drawPoint(int x,int y)
{

        x = x + 512;
	y = 368 - y; 
       
       
       	   
	 XDrawPoint(display, win, gc, x, y);
	//XPutPixel(image, x, y, 0xffffff000000);
	XSync(display, False);
}


void MyDisplay :: getImage(unsigned int width,unsigned int height)
{
	cout << "in trace" << endl;
	image = XGetImage(display,win,0 ,0, width, height, AllPlanes,XYPixmap);
	cout << "out trace" << endl;
}


void MyDisplay :: createGC(int reverseVideo)
{
	unsigned long valueMask = 0;
	XGCValues values;
	unsigned int lineWidth = 2;
	int lineStyle = LineSolid;
	int capStyle = CapButt;
	int joinStyle = JoinBevel;
	
	gc = XCreateGC(display,win,valueMask,&values);
	
	if(gc < 0)
		fprintf(stderr,"XCreateGC: \n");
	
	if (reverseVideo){
    	XSetForeground(display, gc, WhitePixel(display, screenNo));
    	XSetBackground(display, gc, BlackPixel(display, screenNo));
  	}
  	else{
    	XSetForeground(display, gc, BlackPixel(display, screenNo));
    	XSetBackground(display, gc, WhitePixel(display, screenNo));
  	}
	
	XSetLineAttributes(display, gc,
                     lineWidth, lineStyle, capStyle, joinStyle);
	
	XSetFillStyle(display, gc, FillSolid);
	XSync(display, False);
	
}



void MyDisplay :: createWindow( unsigned int width,unsigned int height, int x, int y){
	
	int winBorderWidth = 2;
	win = XCreateSimpleWindow(display,RootWindow(display,screenNo),x,y,width,height,winBorderWidth,
		  BlackPixel(display,screenNo),WhitePixel(display,screenNo));
	
	XMapWindow(display,win);
	XFlush(display);
}



unsigned int MyDisplay :: getWidth()
{
	return displayWidth;
}



unsigned int MyDisplay :: getHeight()
{
	return displayHeight;
}




 Display * MyDisplay :: getDisplay() {

    return display;
}



 GC MyDisplay :: getGC(){

    return gc;
} 

